<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="新闻名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入新闻名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="首页推荐" prop="location">
        <el-select
          v-model="queryParams.location"
          placeholder="请选择首页推荐"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_journalism_home"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上架状态" prop="putawayStatus">
        <el-select
          v-model="queryParams.putawayStatus"
          placeholder="请选择上架状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_data_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="用户端" prop="sources">
        <el-select
          v-model="queryParams.sources"
          placeholder="请选择来源"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_module_sources"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['admin:journalism:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['admin:journalism:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['admin:journalism:remove']"
          >删除</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="journalismList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="新闻名称" align="center" prop="name" />

      <el-table-column label="来源软件" align="center" prop="releaseSource" />

      <!-- <el-table-column label="用户端" align="center" prop="sources">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_module_sources"
            :value="scope.row.sources"
          />
        </template>
      </el-table-column> -->
      <el-table-column label="排序" align="center" prop="weight" />
      <el-table-column label="点击次数" align="center" prop="ctrNum" />
      <el-table-column label="版本" align="center" prop="version" />
      <el-table-column label="首页推荐" align="center" prop="location">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_journalism_home"
            :value="scope.row.location"
          />
        </template>
      </el-table-column>
      <el-table-column label="上架状态" align="center" prop="putawayStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_data_status"
            :value="scope.row.putawayStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="上架时间"
        align="center"
        prop="putawayTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.putawayTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标签" align="center" prop="label" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['admin:journalism:edit']"
            @click="openNewWindow(scope.row)"
            >预览样式</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:journalism:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['admin:journalism:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改新闻管理对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      :close-on-click-modal="false"
      width="1300px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="新闻名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入新闻名称" />
        </el-form-item>
        <el-form-item label="新闻简介" prop="intro">
          <el-input
            v-model="form.intro"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="来源软件" prop="releaseSource">
          <el-input v-model="form.releaseSource" placeholder="请输入来源" />
        </el-form-item>

        <el-form-item label="新闻内容">
          <!-- <editor v-model="form.content" :min-height="250" /> -->
           <Tinymce v-if="open" :height="200" v-model="form.content"></Tinymce>
        </el-form-item>

        <el-form-item label="状态" prop="putawayStatus">
          <el-radio-group v-model="form.putawayStatus">
            <el-radio
              v-for="dict in dict.type.sys_data_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>

        <el-form-item label="标签" prop="labelIds">
          <el-checkbox-group v-model="form.labelIds">
            <el-checkbox
              v-for="dict in LabeList"
              :key="dict.id"
              :label="dict.id"
            >
              {{ dict.name }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <!-- <el-form-item label="来源" prop="sources">
          <el-select v-model="form.sources" placeholder="请选择来源" clearable>
            <el-option
              v-for="dict in dict.type.sys_module_sources"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="首页推荐" prop="location">
          <el-select
            v-model="form.location"
            placeholder="请选择首页推荐"
            clearable
          >
            <el-option
              v-for="dict in dict.type.sys_journalism_home"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="点击次数" prop="ctrNum">
          <el-input
            v-model="form.ctrNum"
            placeholder="请输入点击次数"
            type="number"
            :max="1000000"
            :min="1"
          />
        </el-form-item>
        <el-form-item label="排序" prop="weight">
          <el-input
            v-model="form.weight"
            placeholder="请输入排序"
            type="number"
            :max="10000"
            :min="1"
          />
        </el-form-item>
        <el-form-item label="版本" prop="version">
          <el-input
            v-model="form.version"
            placeholder="请输入版本"
            type="number"
            :max="1000"
            :min="1"
          />
        </el-form-item>
        <el-form-item label="上架时间" prop="putawayTime">
          <el-date-picker
            format="yyyy-MM-dd HH:mm:ss"
            clearable
            v-model="form.putawayTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择上架时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="新闻视频" prop="videoFileId">
          <file-upload
            v-model="videoFileUrl"
            :limit="1"
            :fileSize="50"
            :fileId.sync="form.videoFileId"
          />
        </el-form-item>
        <el-form-item label="新闻图片" prop="imageFileId">
          <image-upload
            v-model="imageFileUrl"
            :limit="1"
            :fileId.sync="form.imageFileId"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listJournalism,
  getJournalism,
  delJournalism,
  addJournalism,
  updateJournalism,
} from "@/api/journalism/journalism";
import { listLabel } from "@/api/label/label";
import Tinymce from "@/components/Tinymce";
export default {
  name: "Journalism",
  dicts: [
    "sys_journalism_home",
    "sys_module_sources",
    "sys_show_hide",
    "sys_data_status",
  ],
  components: {
    Tinymce,
  },

  data() {
    return {
      tiny: {
        value: 'hh',
      },

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 新闻管理表格数据
      journalismList: [],
      //标签列表
      LabeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        name: null,
        location: null,
        putawayStatus: null,
        sources: null,
      },
      // 查询参数
      LableQueryParams: {
        pageNum: 1,
        pageSize: 200,
      },
      // 表单参数
      form: {},
      //图片或文件
      videoFileUrl: "",
      imageFileUrl: "",
      // 表单校验
      rules: {
        name: [
          { required: true, message: "新闻名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getListLabe();
  },
  methods: {
    openNewWindow(row) {
      window.open(
        process.env.VUE_APP_BASE_URL + "/newscenten/details?id=" + row.id,
        "_blank"
      );
    },
    /** 查询新闻管理列表 */
    getList() {
      this.loading = true;
      listJournalism(this.queryParams).then((response) => {
        this.journalismList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getListLabe() {
      listLabel(this.LableQueryParams).then((response) => {
        this.LabeList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      (this.videoFileUrl = ""),
        (this.imageFileUrl = ""),
        (this.form = {
          id: null,
          name: null,
          intro: null,
          content: null,
          location: null,
          ctrNum: null,
          releaseSource: null,
          putawayStatus: null,
          sources: null,
          weight: null,
          version: null,
          putawayTime: null,
          labelIds: [],
          label: [],
          videoFileId: null,
          imageFileId: null,
          imageUrl: null,
          deleted: null,
          releaseTime: null,
          creator: null,
          createTime: null,
          modifyTime: null,
        });
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加新闻管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.LabeList;
      this.reset();
      this.form = row;
      this.open = true;
      this.title = "修改新闻管理";
      this.videoFileUrl =
        row.videoFileId != null ? row.videoFileId.fileAddr : null;
      this.imageFileUrl = row.imageFile != null ? row.imageFile.fileAddr : null;
      const id = row.id || this.ids;
      getJournalism(id).then((response) => {
        this.form.labelIds = response.data.labelIds.split(",").map(Number);
        this.open = true;
        this.title = "修改新闻管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (this.videoFileUrl == "" || this.videoFileUrl == null) {
          this.form.videoFileId = null;
        }
        if (this.imageFileUrl == "" || this.imageFileUrl == null) {
          this.form.imageFileId = null;
        }
        if (valid) {
          this.form.labelIds = this.form.labelIds.join(",");
          if (this.form.id != null) {

            updateJournalism(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addJournalism(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除新闻管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delJournalism(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "admin/journalism/export",
        {
          ...this.queryParams,
        },
        `journalism_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
